package com.mars.module.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mars.common.response.PageInfo;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import com.mars.module.admin.request.AdminInviteRecordRequest;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import com.mars.module.admin.mapper.AdminInviteRecordMapper;
import org.springframework.beans.BeanUtils;
import com.mars.module.admin.entity.AdminInviteRecord;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mars.module.admin.service.IAdminInviteRecordService;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 【请填写功能名称】业务层处理
 *
 * @author mars
 * @date 2024-07-18
 */
@Slf4j
@Service
@AllArgsConstructor
public class AdminInviteRecordServiceImpl implements IAdminInviteRecordService {

    private final AdminInviteRecordMapper baseMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AdminInviteRecord add(AdminInviteRecordRequest request) {
        AdminInviteRecord entity = AdminInviteRecord.builder().build();
        BeanUtils.copyProperties(request, entity);
        baseMapper.insert(entity);
        return entity;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean delete(Integer id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteBatch(List<Integer> ids) {
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean update(AdminInviteRecordRequest request) {
        AdminInviteRecord entity = AdminInviteRecord.builder().build();
        BeanUtils.copyProperties(request, entity);
        return baseMapper.updateById(entity) > 0 ;
    }

    @Override
    public AdminInviteRecord getById(Integer id) {
        return baseMapper.selectById(id);
    }

    @Override
    public PageInfo<AdminInviteRecord> pageList(AdminInviteRecordRequest request) {
        Page<AdminInviteRecord> page = new Page<>(request.getPageNo(), request.getPageSize());
        LambdaQueryWrapper<AdminInviteRecord> query = this.buildWrapper(request);
        IPage<AdminInviteRecord> pageRecord = baseMapper.selectPage(page, query);
        return PageInfo.build(pageRecord);
    }


    @Override
    public List<AdminInviteRecord> list(AdminInviteRecordRequest request) {
        LambdaQueryWrapper<AdminInviteRecord> query = this.buildWrapper(request);
        return baseMapper.selectList(query);
    }

    private LambdaQueryWrapper<AdminInviteRecord> buildWrapper(AdminInviteRecordRequest param) {
        LambdaQueryWrapper<AdminInviteRecord> query = new LambdaQueryWrapper<>();
        if (param.getInviteActivityId() != null){
               query.eq(AdminInviteRecord::getInviteActivityId ,param.getInviteActivityId());
        }
         if (StringUtils.isNotBlank(param.getByUserId())){
               query.like(AdminInviteRecord::getByUserId ,param.getByUserId());
        }
         if (StringUtils.isNotBlank(param.getNickname())){
               query.like(AdminInviteRecord::getNickname ,param.getNickname());
        }
         if (StringUtils.isNotBlank(param.getHeadImage())){
               query.like(AdminInviteRecord::getHeadImage ,param.getHeadImage());
        }
         if (StringUtils.isNotBlank(param.getCreateUserId())){
               query.like(AdminInviteRecord::getCreateUserId ,param.getCreateUserId());
        }
         if (StringUtils.isNotBlank(param.getUpdateUserId())){
               query.like(AdminInviteRecord::getUpdateUserId ,param.getUpdateUserId());
        }
        return query;
    }

}
